package com.aco.party.service.system;

import com.aco.party.dao.custom.CommentsDao;
import com.aco.party.dao.custom.UserDao;
import com.aco.party.enums.VisitTypeEnum;
import com.aco.party.model.po.Comments;
import com.aco.party.model.vo.CommentsParam;
import com.aco.party.model.vo.CommentsVo;
import com.aco.party.model.vo.VisitParam;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.UserWithRoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Service
public class CommentsService {
    @Resource
    private CommentsDao commentsDao;

    @Resource
    private UserDao userDao;

    @Resource
    private VisitService visitService;

    public List<CommentsVo> getByPage(PageParam pageParam,CommentsParam param) {
        List<CommentsVo> list = new ArrayList<>();
        List<Comments> commentss = commentsDao.getList(pageParam,param);
        CommentsVo vo;
        if (commentss != null) {
            for (Comments comments : commentss) {
                vo = new CommentsVo();
                BeanUtils.copyProperties(comments, vo);
                if(vo.getCommentApproved() != null){
                    if(vo.getCommentApproved() == 0) vo.setCommentsApprovedStr("隐藏");
                    if(vo.getCommentApproved() == 1) vo.setCommentsApprovedStr("显示");
                }
                if(vo.getCommentAuthor() != null){
                    UserWithRoleVo user = userDao.getUserWithRole(vo.getCommentAuthor());
                    if(user != null) {
                        vo.setCommentAuthorName(user.getNickName());
                        vo.setCommentAuthorPhoto(user.getPhotoUrl());
                    }
                }

                VisitParam visitParam = new VisitParam();
                visitParam.setType(VisitTypeEnum.评论点赞.getValue());
                visitParam.setTargetId(vo.getId());
                vo.setLikes(visitService.getCountByParam(visitParam));
                list.add(vo);
            }
        }
        return list;
    }

    public Integer getTotal(CommentsParam param) {
        return commentsDao.getTotal(param);
    }

    public void update(Comments comments) {
        comments.setUpdateTime(new Date());
        commentsDao.updateByPrimaryKeySelective(comments);
    }

    public Integer add(Comments comments) {
        comments.setCreateTime(new Date());
        return commentsDao.insertSelective(comments);
    }

    public void delete(Long id) {
        commentsDao.deleteByPrimaryKey(id);
    }

    public Comments getByPrimaryKey(Long id) {
        return commentsDao.selectByPrimaryKey(id);
    }

    public CommentsVo getVoById(Integer id){
        Comments comments = getByPrimaryKey((long)id);
        CommentsVo vo = null;
        if(comments != null){
            vo = new CommentsVo();
            BeanUtils.copyProperties(comments, vo);
            if(vo.getCommentApproved() != null){
                if(vo.getCommentApproved() == 0) vo.setCommentsApprovedStr("隐藏");
                if(vo.getCommentApproved() == 1) vo.setCommentsApprovedStr("显示");
            }
            if(vo.getCommentAuthor() != null){
                UserWithRoleVo user = userDao.getUserWithRole(vo.getCommentAuthor());
                if(user != null) {
                    vo.setCommentAuthorName(user.getNickName());
                    vo.setCommentAuthorPhoto(user.getPhotoUrl());
                }
            }
            VisitParam visitParam = new VisitParam();
            visitParam.setTargetId(vo.getId());
            visitParam.setType(VisitTypeEnum.问答点赞.getValue());
            vo.setLikes(visitService.getCountByParam(visitParam));
        }

        return vo;
    }
}
